package com.qk.management.service.impl;

import com.qk.common.PageResult;
import com.qk.entity.Dept;
import com.qk.entity.Role;
import com.qk.management.mapper.RoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class RoleServiceImpl implements com.qk.management.service.RoleService {
    @Autowired
    private RoleMapper roleMapper;


    @Override
    public PageResult<Role> page(String name, String label, Integer page, Integer pagesize) {
        Integer total = roleMapper.count(name,label);
        Integer offset = (page-1)*pagesize;
        List<Role> rows = roleMapper.selectByPage(name,label,offset,pagesize);
        return PageResult.<Role>builder().total(total).rows(rows).build();
    }

    @Override
    public void deleteById(Integer id) {
        roleMapper.deleteById(id);
    }

    @Override
    public void update(Role role) {
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.update(role);
    }

    @Override
    public Role getById(Integer id) {
        Role role = roleMapper.selectById(id);
        return role;
    }

    @Override
    public void addRole(Role role) {
        role.setCreateTime(LocalDateTime.now());
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.insert(role);
    }

    // 查询所有角色
    @Override
    public List<Role> pagelist() {
        List<Role> rows = roleMapper.selectAll();
        return rows;
    }
}
